# coding:utf-8
import re

from sentiment.senti_dict.dictSentiment import DictSentiment
from sentiment.snownlp_textblob.beiyesSentiment import BeiyesSentiment
from sentiment.word_freq.wordFreq import word_freq


class ArticleOutlineService:
    # 基于情感词典，统计学
    @staticmethod
    def dictSentiment(text):
        '''
        基于情感词典
        :param text: 一段文本整体
        :return: {code:0, data:(pos,neg)}
        '''
        # 分词
        ps_score, ng_score = DictSentiment.sentiment(text)
        freq = word_freq(text, 10) # [{'':10}]
        freq_res = [[list(item)[0] for item in freq],[item[list(item)[0]] for item in freq]]
        return {'code':0, 'data':{'sentiment':{'pos':ps_score,'neg':ng_score},
                                  'freq':freq_res}}

    # 基于情感词典
    @staticmethod
    def beiyesSentiment(text):
        '''
        基于情感词典
        :param text: 多个句子
        :return: {code:0, data:[{'sentence':'', sentiment:0.9},
                               {'sentence':'', sentiment:0.8},
                               ],}
        '''
        # 词频
        freq = word_freq(text, 10)  # [{'':10}]
        freq_res = [[list(item)[0] for item in freq], [item[list(item)[0]] for item in freq]]
        # 句子拆分
        data = []
        sentences = re.split('[。！~？!?…]', text)
        pos = 0
        neg = 0
        for s in sentences:
            sentiment_grade = BeiyesSentiment.sentiment(s)
            data.append({'sentence':s, 'sentiment':sentiment_grade})
            pos += sentiment_grade
            neg += (1-sentiment_grade)
        return {'code':0, 'data':{'freq':freq_res, 'sentiment':{'pos':pos, 'neg':neg}}}


# ArticleOutlineService.dictSentiment('a训练。范德萨发！范德对对对.dsa第三方')